import axios, {
  AxiosInstance,
  AxiosRequestConfig,
  AxiosResponse,
  AxiosError,
  InternalAxiosRequestConfig,
} from "axios";
import useUsersStore from "@/store/useUsersStore";
// const redirtURL = import.meta.env.VITE_REDITURL;
// console.log("redirtURL ", redirtURL);
export interface ResultData<T> {
  code: number;
  msg: string;
  data: T;
}
class HttpRequest {
  private readonly baseURL: string;
  private readonly withCredentials: boolean;
  private readonly timeout: number;
  private readonly instance: AxiosInstance;
  constructor() {
    //获取当前环境的api地址
    this.baseURL = import.meta.env.VITE_BASE_URL as string;
    this.withCredentials = true;
    this.timeout = 1000 * 60;
    this.instance = axios.create({
      baseURL: this.baseURL,
      withCredentials: this.withCredentials,
      timeout: this.timeout,
    });
    this.interceptors(this.instance);
  }
  private interceptors(instance: AxiosInstance) {
    instance.interceptors.request.use(
      (config: InternalAxiosRequestConfig<any>) => {
        // const token = sessionStorage.getItem("token");
        const usersStore = useUsersStore();
        const token = usersStore.token;
        console.log("token", token)
        const whiteList = ["/login", "/resigister"];
        console.log("config: ", config.url);
        if (token && !whiteList.includes(config.url)) {
          config.headers.Authorization = `Bearer ${token}`;
        }
        return config;
      },
      (error) => Promise.reject(error + "请求错误")
    );

    instance.interceptors.response.use(
      (response: AxiosResponse) => {
        if (response.status === 200) {
          return response.data;
        } else {
          console.log(" response: ", response);
          return Promise.reject(response);
        }
      },
      (error: AxiosError) => {
        return Promise.reject(error.response?.data);
      }
    );
  }
  request<T>(config: AxiosRequestConfig) {
    return this.instance.request<ResultData<T>>(config);
  }

  get<T, K>(url: string, params?: K, _object = {}): Promise<ResultData<T>> {
    return this.instance({ url, params, ..._object, method: "GET" });
    // return this.instance.get<ResultData<T>>(url, { params, ..._object })
  }
  post<T, K>(url: string, data?: K, _object = {}): Promise<ResultData<T>> {
    // console.log("config", { url, data, ..._object, method: "POST" });
    return this.instance({ url, data, ..._object, method: "POST" });
    // return this.instance.post<ResultData<T>>(url, data, { ..._object })
  }
  put<T, K>(url: string, data?: K, _object = {}): Promise<ResultData<T>> {
    return this.instance({ url, data, ..._object, method: "PUT" });
    // return this.instance.put<ResultData<T>>(url, data, { ..._object })
  }
  delete<T, K>(url: string, data?: K, _object = {}): Promise<ResultData<T>> {
    return this.instance({ url, data, ..._object, method: "DELETE" });
    // return this.instance.delete<ResultData<T>>(url, { data, ..._object })
  }
}
const http = new HttpRequest();
export default http;
